package neusoft.yuejian_11.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import java.util.List;

import neusoft.yuejian_11.activity.ClubSearchActivity;
import neusoft.yuejian_11.adapter.ClubListAdapter;
import neusoft.yuejian_11.base.BaseFragment;
import neusoft.yuejian_11.bean.ClubBean;
import neusoft.yuejian_11.listener.RetrofitListener;
import neusoft.yuejian_11.model.ClubModel;
import neusoft.yuejian_11.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class ClubListFragment extends BaseFragment implements RetrofitListener{
    private RecyclerView recyclerView;
    private ClubListAdapter adapter;
    private ImageButton imageButtonSearch;


    public ClubListFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_club_list,container,false);
        imageButtonSearch = view.findViewById(R.id.imageButtom_search);
        recyclerView =(RecyclerView)view.findViewById(R.id.recyclerView);

        initEvents();//点击事件处理
        initData();//网络获取
        return view;
    }

    private void initEvents() {
        imageButtonSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(getActivity().getApplicationContext(), ClubSearchActivity.class);
                getActivity().startActivity(intent);
            }
        });
    }

    private void initData() {
        ClubModel clubModel = new ClubModel();
        clubModel.clubList(this);
    }


    @Override
    public void onSuccess(Object o, int flag) {
        if (getActivity() != null) {
            List<ClubBean> clubBeanList = (List<ClubBean>)o;
            adapter = new ClubListAdapter(getActivity(), clubBeanList);
            LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(layoutManager);
            recyclerView.setAdapter(adapter);
            recyclerView.setItemAnimator(new DefaultItemAnimator());
        }
    }

    @Override
    public void onFail() {

    }

}
